package com.pulaien.web.controller.customersetup;

import com.pulaien.common.annotation.Anonymous;
import com.pulaien.common.core.controller.BaseController;
import com.pulaien.common.core.domain.R;
import com.pulaien.common.core.domain.entity.SysUser;
import com.pulaien.common.domain.ResponseCode;
import com.pulaien.common.utils.ConstantsUtil;
import com.pulaien.common.utils.MessageUtils;
import com.pulaien.common.utils.SecurityUtils;
import com.pulaien.system.domain.Customer;
import com.pulaien.system.domain.dto.ExtCustomer;
import com.pulaien.system.domain.dto.customersetup.AdminModifyParam;
import com.pulaien.system.service.ICustomerService;
import com.pulaien.system.service.customersetup.AdminUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yuanlai
 * @create 20230411 19:19
 **/
@Api(tags = "客户设置", description = "买家端-客户设置")
@RestController
@Anonymous
public class CustomerSetUpController extends BaseController {


    private final AdminUserService adminUserService;
    private final ICustomerService customerService;

    private final StringRedisTemplate stringRedisTemplate;


    public CustomerSetUpController(
            AdminUserService adminUserService,
            ICustomerService customerService,
            StringRedisTemplate stringRedisTemplate) {
        this.adminUserService = adminUserService;
        this.customerService = customerService;
        this.stringRedisTemplate = stringRedisTemplate;
    }


    @ApiOperation(value = "修改客户个人信息")
    @PostMapping(value = "/shop/admin/modifyInformation")
    public R modifyInformation(
            @RequestBody AdminModifyParam modifyParam
    ) {
        return adminUserService.modifyInfo(modifyParam);
    }


    @ApiOperation(value = "修改客户邮箱(根据新邮箱验证码重置邮箱)")
    @PostMapping(value = "/shop/admin/modifyEmail")
    public R modifyEmail(
            @RequestBody AdminModifyParam modifyParam
    ) {

        if(null == modifyParam.getPassword()) {
            return R.fail(MessageUtils.getMessage(10038));
        }


        String code = modifyParam.getCode();
        String email = modifyParam.getEmail();
        String password = modifyParam.getPassword();

        String oldPassword = SecurityUtils.getLoginUser().getUser().getPassword();
        if (!SecurityUtils.matchesPassword(password, oldPassword)) {
            return R.fail("密码校验失败");
        }

        String validCode = stringRedisTemplate.opsForValue().get(ConstantsUtil.VERIFY_CODE_PREFIX + email);
        //String validCode = session.getAttribute(ConstantsUtil.FREEBACK_KEY) == null ? "" : session.getAttribute(ConstantsUtil.FREEBACK_KEY).toString();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(code) && StringUtils.isNotBlank(validCode) && code.toLowerCase().equals(validCode.toLowerCase())) {
            return adminUserService.modifyEmail(modifyParam);
        }
        return R.fail(MessageUtils.getMessage(10304));

    }


    @ApiOperation(value = "获取用户信息")
    @GetMapping(value = "/shop/admin/getCustomerInfo")
    public R<Customer> getCustomerInfo() {
        SysUser user = adminUserService.getCurrentUser().getUser();
        ExtCustomer extCustomer = new ExtCustomer();
        Customer customer = customerService.getCustomerByUserId(user.getUserId());
        if (customer == null) {
            return R.fail(MessageUtils.getMessage(ResponseCode.GET_NOT_INFORMATION_FAIL));
        }
        BeanUtils.copyProperties(customer,extCustomer);
        extCustomer.setAvatar(user.getAvatar());
        return R.ok(customer);
    }


/*    @ApiOperation(value = "列表分页展示")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前记录起始索引", paramType = "query", defaultValue = "1", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", paramType = "query", defaultValue = "10", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "orderByColumn", value = "排序列", paramType = "query"),
            @ApiImplicitParam(name = "isAsc", value = "排序的方向 \"desc\" 或者 \"asc\"", paramType = "query"),
            @ApiImplicitParam(name = "reasonable", value = "分页参数合理化", paramType = "query", dataType = "Boolean")
    })
    @GetMapping(value = "/api/cms/configAddressInfo/list")
    public TableDataInfo list(
            ExtConfigAddressInfo company,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum
    ) {
        startPage();
        List<ExtConfigAddressInfo> record = addressInfoService.list(company, pageNum, pageSize);
        return getDataTable(record);
    }


    @ApiOperation("新增和编辑")
    @PostMapping(value = "/api/cms/configAddressInfo/insertAndEdit")
    public R insertAndEdit(
            @RequestBody ConfigAddressInfo company
    ) {
        return addressInfoService.insertAndEdit(company);
    }


    @ApiOperation(value = "地址下拉选项")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前记录起始索引", paramType = "query", defaultValue = "1", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", paramType = "query", defaultValue = "10", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "orderByColumn", value = "排序列", paramType = "query"),
            @ApiImplicitParam(name = "isAsc", value = "排序的方向 \"desc\" 或者 \"asc\"", paramType = "query"),
            @ApiImplicitParam(name = "reasonable", value = "分页参数合理化", paramType = "query", dataType = "Boolean")
    })
    @GetMapping(value = "/api/cms/configAddressInfo/getAllOrderList")
    public TableDataInfo list(
            ExtConfigAddressInfo company
    ) {
        startPage();
        LoginUser user = this.adminUserService.getCurrentUser();
        company.setUserId(user.getUserId());
        List<ConfigAddressInfo> record = addressInfoService.getAllOrderList(company);
        return getDataTable(record);
    }*/


}
